angular.module('charity.donate', [])
    .controller('donateCtr', ['$scope', '$filter', 'donateService', '$routeParams', '$uibModal', 'logger',
        function ($scope, $filter, donateService, $routeParams, $uibModal, logger) {
            $scope.stores = [];
            $scope.searchKeywords = '';
            $scope.filteredStores = [];
            $scope.row = '',

            $scope.select = function (page) {
                var end, start;
                start = (page - 1) * $scope.numPerPage;
                end = start + $scope.numPerPage;
                return $scope.currentPageStores = $scope.filteredStores.slice(start, end);
            };

            $scope.onFilterChange = function () {
                $scope.select(1);
                $scope.currentPage = 1;
                return $scope.row = '';
            };

            $scope.onNumPerPageChange = function () {
                $scope.select(1);
                return $scope.currentPage = 1;
            };

            $scope.onOrderChange = function () {
                $scope.select(1);
                return $scope.currentPage = 1;
            };

            $scope.changeMenu = function () {
                $scope.search();
            }

            $scope.search = function () {
                donateService.queryAllDonates().then(function (results) {

                    if (!results || results.code != 0) {
                        logger.logError((results && results.message) || '查询数据失败,请重新登录');
                        return;
                    }
                    $scope.stores = results.data || [];

                    $scope.filteredStores = $filter('filter')($scope.stores, $scope.searchKeywords);
                    return $scope.onFilterChange();
                });
            };

            $scope.order = function (rowName) {
                if ($scope.row === rowName) {
                    return;
                }
                $scope.row = rowName;
                $scope.filteredStores = $filter('orderBy')($scope.stores, rowName);
                return $scope.onOrderChange();
            };
            $scope.numPerPageOpt = [3, 5, 10, 20];
            $scope.numPerPage = $scope.numPerPageOpt[2];
            $scope.currentPage = 1;
            $scope.currentPageStores = [];

            $scope.deleteDonate = function (donate) {
                var modalInstance = $uibModal.open({
                    animation: true,
                    templateUrl: 'deleteModalDonate.html',
                    controller: 'deleteComponentCtrl',
                    resolve: {
                        data: function () {
                            return {id: donate.id, name: donate.name}
                        }
                    }
                });

                modalInstance.result.then(function (donateId) {
                    donateService.deleteDonate(donateId).then(function (results) {
                        if (results && results.code == 0) {
                            logger.logSuccess('操作成功');
                            $scope.search();

                        }
                        else {
                            logger.logError((results && results.message) || '操作失败,请重新登录');
                        }
                    })
                })
            };

            $scope.search();
        }])

    .controller('editDonateCtrl', ['$scope', '$commonService','donateService', '$routeParams', 'logger', 'cfpLoadingBar','$location',
        function ($scope, $commonService, donateService, $routeParams, logger, cfpLoadingBar, $location) {
            $scope.donate = {};
            $scope.saveDonate = function () {
                donateService.addDonate($scope.donate).then(function (results) {
                    if (!results || results.code != 0) {
                        logger.logError((results && results.message) || '操作数据失败,请重新登录');
                        return;
                    }
                    else{
                        logger.logSuccess('保存数据成功!');
                        $location.path('/donates/');
                    }
                })
            }

        }])
    .controller('deleteComponentCtrl', ['$scope', '$uibModalInstance', 'data',
        function ($scope, $uibModalInstance, data) {
            $scope.data = data;
            $scope.ok = function () {
                $uibModalInstance.close($scope.data.id);
            };
            $scope.cancel = function () {
                $uibModalInstance.dismiss("cancel");
            };

        }])
    .factory('donateService', ['$http', function ($http) {
        return {
            queryAllDonates: function () {
                return $http({
                    method: "GET",
                    url: "/management/donates"
                }).then(function (results) {
                    return results.data;
                })
            },
            getDonate: function (donateId) {
                return $http({
                    method: "GET",
                    url: "/management/donates/" + donateId,
                }).then(function (results) {
                    return results.data;
                })
            },
            addDonate: function (donate) {
                return $http({
                    method: "POST",
                    url: "/management/donates/",
                    data: donate
                }).then(function (results) {
                    return results.data;
                })
            },
            deleteDonate: function (donateId) {
                return $http({
                    method: "DELETE",
                    url: "/management/donates/" + donateId
                }).then(function (results) {
                    return results.data;
                })
            }
        }
    }]);